package com.colis.quartz.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * <p> 描述：设置swaggerApi 文档 </p>
 *
 * @author wlp
 * @version 1.0.0
 * @date 2022/1/12 17:09
 */
@Configuration
@EnableOpenApi
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    /**
     * <p> 方法描述: 创建文档扫描属性相关配置项 </p>
     *
     * @return Docket
     * @author wlp
     * @date 2021/10/16 14:10
     */
    @Bean
    public Docket createRestApi() {
        //返回文档摘要信息
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class)) 扫描特定注解
                // 指定接口所在包路径
//                .apis(RequestHandlerSelectors.basePackage("com.colis.video.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .groupName("colis-video")
                // 设置接口全局参数
                .globalRequestParameters(getGlobalRequestParameters());
    }

    /**
     * <p> 方法描述：生成接口信息，包括标题、联系人等 </p>
     *
     * @return ApiInfo
     * @author wlp
     * @date 2021/10/16 14:10
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("定时任务接口管理")
                .description("定时任务接口管理，如有疑问，请联系管理员 ,博客：https://blog.csdn.net/WLPJLP")
                .contact(new Contact("526412864@qq.com", "https://blog.csdn.net/WLPJLP", "526412864@qq.com"))
                .version("1.0")
                .build();
    }

    /**
     * <p> 方法描述：生成全局通用参数 </p>
     *
     * @return List<RequestParameter>
     * @author wlp
     * @date 2021/6/12 10:53
     */
    private List<RequestParameter> getGlobalRequestParameters() {
        return List.of(new RequestParameterBuilder()
                .name("version")
                .description("客户端的版本号")
                .required(false)
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build(), new RequestParameterBuilder()
                .name("token")
                .description("接口验证token")
                .required(false)
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build());
    }
}